package hotel.switchs.oversea.online.controller;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.Result;
import hotel.switchs.oversea.api.VO.SwitchCityInfoVo;
import hotel.switchs.oversea.api.VO.request.SwitchHotelCityRedisParamsVo;
import hotel.switchs.oversea.online.service.SwitchCityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author lmf
 * @Date 2020/5/13 13:39
 * 携程城市控制器
 **/
@Slf4j
@RestController
@RequestMapping("/switchCity")
@Api(value = "switchCityController",tags = {"获取城市信息接口"})
public class SwitchCityInfoController {

    @Autowired
    SwitchCityInfoService switchCityInfoService;

    @PostMapping("/getPageHotelIds")
    @ApiOperation(value = "分页获取城市id",notes="必须经过auth授权")
    public Result<Object> queryPageHotelIds(@RequestBody JSONObject jsonObject){
        //参数校验
        int index = jsonObject.get("index")==null?-1:(int)jsonObject.get("index");
        int pageSize = jsonObject.get("pageSize")==null?-1:(int)jsonObject.get("pageSize");
        if(-1 == index){
            return new Result<>("index不可为空");
        }
        if(-1 == pageSize){
            return new Result<>("pageSize不可为空");
        }
        //发起请求并保存数据
        return  switchCityInfoService.queryPageCityIds(index,pageSize);
    }

    /**
     * 获取城市列表数据
     */
    @GetMapping("/getLists")
    @ApiOperation(value = "获取城市列表",notes = "必须经过oauth2授权")
    public Result<Object> getCityLists(@RequestParam(value = "countryID")int countryID, @RequestParam(value = "pageSize",defaultValue = "15")int pageSize, @RequestParam(value = "currentPageIndex",defaultValue = "1")int currentPageIndex){

        if(countryID<1){
            return new Result<>("国家id不合法");
        }
        return switchCityInfoService.getcitylist(countryID,pageSize,currentPageIndex);

    }

    @PostMapping("/saveCityInfos")
    @ApiOperation(value = "保存酒店信息接口",notes="必须经过auth授权")
    public Result<Object> saveHotelInfos(@RequestBody SwitchHotelCityRedisParamsVo switchHotelRedisParamsVo){
        //发起请求并保存数据
        Map<String,List<SwitchCityInfoVo>> switchCityInfoVosMap = switchHotelRedisParamsVo.getSwitchCityInfoVosMap();
        if(null == switchCityInfoVosMap || switchCityInfoVosMap.size()==0){
            return new Result<>("参数switchCityInfoVosMap不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchCityInfoService.saveCityInfos(switchCityInfoVosMap);
    }

    /**
     * @explain test
     * @return  Boolean
     */
    @PostMapping("/getList")
    public Result<Object> getList(){
        return new Result<>(switchCityInfoService.selectAll());
    }
}
